package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.MenuDto;
import com.hualude.roadsign.repository.MenuRepository;
import com.hualude.roadsign.repository.OrgRepository;
import com.hualude.roadsign.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by SN on 2017/2/18.
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuRepository menuRepository;

    @Resource
    private OrgRepository orgRepository;

    @Override
    public List<MenuDto> getMenuByIds(String menuIds, Integer userId, Integer orgId, Integer orgType) throws Exception {
        String menuIds2 = "1000,1002,2000,2003,3000,3003,4000,5000," +
                "6000,6001,6002,6003,7000,7001,";
        String mMenu = orgRepository.getOrgMenu(orgId);
        String[] menuList = mMenu.split(",");
        for (String mList : menuList) {
            if (mList.equals("1")) {
                menuIds2 += "3001,3002,4001,4002,4003,5001,5002,5003,";

            } else if (mList.equals("2")) {
                menuIds2 += "3008,3009,4010,4011,4012,5011,5012,5029,";

            } else if (mList.equals("3")) {
                menuIds2 += "3006,3007,4007,4008,4009,5008,5009,5030,";

            } else if (mList.equals("4")) {
                menuIds2 += "3004,3005,4004,4005,4006,5005,5006,5031,";

            } else if (mList.equals("5")) {
                menuIds2 += "3020,3021,4025,4026,4027,5026,5027,5028,";

            } else if (mList.equals("6")) {
                menuIds2 += "3014,3015,4019,4020,4021,5020,5021,";

            } else if (mList.equals("7")) {
                menuIds2 += "3016,3017,4022,4023,4024,5023,5024,";

            } else if (mList.equals("8")) {
                menuIds2 += "3010,3011,4013,4014,4015,5014,5015,5032,";

            } else if (mList.equals("9")) {
                menuIds2 += "3012,3013,4016,4017,4018,5017,5018,";

            } else if (mList.equals("10")) {
                menuIds2 += "3018,3019,";
            }
        }
        menuIds2 = menuIds2.substring(0, menuIds2.length() - 1);
        if (menuIds.endsWith(",")) {
            menuIds = menuIds.substring(0, menuIds.length() - 1);
        }
        List<MenuDto> menuDtos = menuRepository.getMenuByIds(menuIds, menuIds2, orgType);
        List<MenuDto> powerDtos = menuRepository.getPowerByUserId(userId);
        for (int i = 0; i < menuDtos.size(); i++) {
            for (int j = 0; j < powerDtos.size(); j++) {
                if (menuDtos.get(i).getMenuId().equals(powerDtos.get(j).getMenuId())) {
                    menuDtos.get(i).setPowerInsert(powerDtos.get(j).getPowerInsert());
                    menuDtos.get(i).setPowerDelete(powerDtos.get(j).getPowerDelete());
                    menuDtos.get(i).setPowerUpdate(powerDtos.get(j).getPowerUpdate());
                    menuDtos.get(i).setPowerSelect(powerDtos.get(j).getPowerSelect());
                    menuDtos.get(i).setPowerCheck(powerDtos.get(j).getPowerCheck());
                }
            }
        }
        return menuDtos;
    }
}
